layui.define(['jquery', 'layer'], function(exports) {
	var $ = layui.jquery,
		layer = layui.layer;
	var signin = function() {
		this.options = {
			elem: null,
			button:{size:8},
			sign_url: null,
			data_url: null,
			showWeek: true,//是否显示星期
			tableElem: null,
			prev: null,
			next: null,
			anim: 5,
			bindCall: ""
		}
	};
	
	signin.prototype.init = function() {
        var _this = this,elem=null,options = _this.options;
        elem = options['elem'] ? options['elem'] : "#signin",
		tableElem = options['tableElem'] ? options['tableElem'] : "#tableSignin",
		sign_url =options['sign_url']

        if(options['button'])
        {
            if(options['button'] ===true) options['button'] = {};
            var buttonsize = options['button']['size'] ? options['button']['size'] : 8;
            var buttontext = options['button']['text'] ? options['button']['text'] : "签到";
            var buttontext_ = options['button']['text_'] ? options['button']['text_'] : "已签到";
            var buttonradius = options['button']['radius'] ? options['button']['radius'] : "100%";
        	var elemString="";
        	elemString = '<div style="display: flex;justify-content: center;align-items: center;width:'+buttonsize+'rem;height: '+buttonsize+'rem;border-radius: '+buttonradius+';-webkit-border-radius: '+buttonradius+';background: rgba(255, 255, 255, 0.4);margin: 1rem auto 0 auto">'
			elemString += '<div style="display: flex;justify-content: center;align-items: center;width:'+(buttonsize-1)+'rem;height: '+(buttonsize-1)+'rem;border-radius: '+buttonradius+';-webkit-border-radius: '+buttonradius+';background: #ffffff;margin: 0 auto;">'
			elemString +='<div class="signBtn" style="display: flex;flex-direction: column;justify-content: center;align-items: center;width:'+(buttonsize-1.8)+'rem;height: '+(buttonsize-1.8)+'rem;border-radius:'+buttonradius+';-webkit-border-radius: '+buttonradius+';border: 1px #7ebdff solid;">'
			elemString +='<strong id="sign-txt" style="height: 3.5rem;line-height: 4.5rem;font-weight: 600;border-bottom: 1px #eee solid;  display: block;width: 85%;margin: 0 auto;text-align: center;color: #007aff;">'+buttontext+'</strong>'
			elemString +='<span style="display: block;width: 85%;margin: 0 auto;text-align: center;color: #007aff;height: 2.5rem;line-height: 2.5rem;font-size: .9rem;line-height: 2rem !important;">连续<em id="sign-count">0</em>天</span>'
			elemString +='</div></div></div>';

        	$(elem).html(elemString);
        	
        	$(".signBtn").on('click',function(){
        		 $.getJSON(sign_url, function(data){
					   
				       if(data.err == 0)
				       {
				       	    $("#sign-txt").html(buttontext_);
				       	    layer.msg("签到成功");
							_this.dateRender();
				       }
				       else
				       {
				       	    layer.msg(data.content);
				       }
				 });
        		
        	});
        	
        	
		

        	_this.dateRender();
        }
        
        
    }
	
	signin.prototype.render = function(options) {
        var _this = this;
        _this.options = $.extend({}, _this.options, options);
        
        _this.init();
        
    }
	
	signin.prototype.dateRender = function(dateV) {
		var _this = this,options = _this.options,
        data_url =options['data_url'],
		prev = options['prev'] ? options['prev'] : "《",
		next = options['next'] ? options['next'] : "》";
        var d = new Date(),year = d.getFullYear(), month = d.getMonth(),day = d.getDate(),nextM=0,prevM=0,nextY=0,prevY=0;
		var param={};

        if(dateV)
		{
			year = dateV.year
			month = dateV.month
			
			var  last_day = new Date(year,month,0);  
			
			param['startTime']  = (new Date(year +"/"+(month+1)+"/1 00:00:00")).getTime()/1000;
			param['endTime']  = (new Date(year +"/"+(month+1)+"/"+last_day.getDate()+" 00:00:00")).getTime()/1000 - 1;
		}
		
		if( month >= 11)
		{
			nextM = 0;
			nextY = year + 1;
			prevY = year;
			prevM = month - 1;
		}
		else if( month <=0)
		{
			 prevM = 11
			 prevY = year-1;
			 nextM = month + 1;
			 nextY = year ;
		}
		else
		{
			nextY = prevY = year;
			prevM = month - 1;
			nextM = month + 1;
			
		}
		
		month = parseInt(month, 10);
        
		month_ = month + 1
		
        var d_ = new Date(year,month_,0)

        var dayNum = d_.getDate(); //某月总天数

        var firstday = new Date(year, month, 1);
        
        var week1 = firstday.getDay();
        
        var dateArray=[];
        
        var offset = week1 - 0;
        var str="";
        var day = 1;
		var tdId="";
		
		 month = month_ < 10 ? "0"+(month_) : (month_);
		
		var tableString = '<table width="100%" height="40" bgcolor="#FFFFFF"><tr><td align="right" id="signPrev">'+prev+'</td><td align="center">'+year+'-'+month+'</td><td align="left" id="signNext">'+next+'</td></tr></table><table cellpadding="0" cellspacing="0" class="layui-table" >'
		
		if(options['showWeek'])
		{		
			 tableString +='<tr><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr>';
		}
			
		tableString +='<tbody id="idCalendar"></tbody></table>';		
		$(tableElem).html(tableString);
		
		dayNum = dayNum + offset;
        for(var i = 1;i<=dayNum;i++)
        {
        	if(i -1 <  offset)
        	{
        		str += "<td> </td>"
        	}
        	else
        	{
				day_ = day < 10 ? "0"+day : day;
				
				tdId = year + "_" + month + "_" + day_;
				
        		str += "<td align='center' valign='middle' id='"+tdId+"'>"+day+"</td>"
        		day++;

        	}
        	
        	if(i % 7 == 1)  
        	{
        		str = "<tr>"+str;
        	}
        	else if(i % 7 == 0)
        	{
        		str =str+"</tr>";
        	}
        }

        $("#idCalendar").html(str)
		
		console.log(param) 

		
	    $.getJSON(data_url,param, function(data){
			 if(data.err == 0)
			 {
				  $('#sign-count').html(data.content.days);
				  
				  var sign = data.content.sign;
				  
				  if(sign)
				  {
					   for(var i in sign)
					   {
						    html = $('#'+i).html();
						    $('#'+i).html('<span style="background:#900;display: flex;justify-content: center;align-items: center;padding:5;border-radius: 100%;-webkit-border-radius:100%; width:30px; height:30px; color:#FFF">'+html+'</span>')
					   }
				  }

			 }
			 else
			 {
				  layer.msg("获取数据失败");
			 }
	    });	
		
		$("#signPrev").on('click',function(){
			 _this.dateRender({year:prevY,month:prevM})
		})
		
		$("#signNext").on('click',function(){
			_this.dateRender({year:nextY,month:nextM})
		})		
		  
    }
	
	var signin = new signin();

	exports('signin', signin); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});